﻿using AutoMapper;
using B.S.BsicData.Api.Read.Application.Command;
using B.S.BsicData.Api.Read.DTO;
using B.S.BsicData.Domain;
using B.S.BsicData.ErrorCode.Entity;
using B.S.BsicData.Infartrans;
using MediatR;

namespace B.S.BsicData.Api.Read.Application.CommandHandler
{
    public class QueryWhouseNumCommandHandler : IRequestHandler<QueryWhouseNumCommand, ApiPaging<WHouseLocaDTO>>
    {
        private readonly IBaseReository<WHouseModeles> whRepo;
        private readonly IBaseReository<LocationModeles> loRepo;
        private readonly IBaseReository<WHouseLocationModel> wlRepo;
        private readonly IMapper mapper;

        public QueryWhouseNumCommandHandler(IBaseReository<WHouseModeles> whRepo, IBaseReository<LocationModeles> loRepo, IBaseReository<WHouseLocationModel> wlRepo, IMapper mapper)
        {
            this.whRepo = whRepo;
            this.loRepo = loRepo;
            this.wlRepo = wlRepo;
            this.mapper = mapper;
        }

        public async Task<ApiPaging<WHouseLocaDTO>> Handle(QueryWhouseNumCommand request, CancellationToken cancellationToken)
        {
            ApiPaging<WHouseLocaDTO> result = new ApiPaging<WHouseLocaDTO>();
            var model = from a in whRepo.GetAll()
                        join b in wlRepo.GetAll() on a.WhouseID equals b.WhouseID
                        join c in loRepo.GetAll() on b.LocationID equals c.LocationID
                        where a.WhouseNum==request.WhouseNum
                        select new WHouseLocaDTO
                        {
                            WhouseID = a.WhouseID,
                            WhouseNum = a.WhouseNum,
                            WhouseName = a.WhouseName,
                            Capacity = a.Capacity,
                            DementperId = a.DementperId,
                            Manager = a.Manager,
                            Phone = a.Phone,
                            WHouseAddr = a.WHouseAddr,
                            Sort = a.Sort,
                            State = a.State,
                            Lock = a.Lock,
                            Rember = a.Rember,
                            CreatePeople = a.CreatePeople,
                            CreateDateTime = a.CreateDateTime,
                            UpdatePeople = a.UpdatePeople,
                            UpdateDateTime = a.UpdateDateTime,
                            LocationID = c.LocationID,
                            LocaTionNum = c.LocaTionNum,
                            LocaTionName = c.LocaTionName,
                            LocaTionCapacity = c.LocaTionCapacity,
                            LocaTionManage = c.LocaTionManage,
                            LocaTionCStatus = c.LocaTionCStatus,
                            LocaTionSort = c.LocaTionSort,
                            LocaTionRember = c.LocaTionRember,
                            LocaCreatePeople= c.LocaCreatePeople,
                            LocaCreateDateTime= c.LocaCreateDateTime,
                            LocaUpdatePeople= c.LocaUpdatePeople,
                            LocaUpdateDateTime= c.LocaUpdateDateTime, 
                        };
            if (!string.IsNullOrEmpty(request.LocaTionNum))
            {
                model = model.Where(p => p.LocaTionNum.Contains(request.LocaTionNum)) ;
            }
            if (!string.IsNullOrEmpty(request.LocaTionName))
            {
                model = model.Where(p=> p.LocaTionName.Contains(request.LocaTionName));
            }
            if (request.LocaTionManage!=null)
            {
                model = model.Where(p => p.LocaTionManage == request.LocaTionManage);
            }
            if (!string.IsNullOrEmpty(request.LocaTionCStatus))
            {
                model = model.Where(p => p.LocaTionCStatus==request.LocaTionCStatus);
            }
            result.TotalCount = model.Count();
            result.TotalPage = (int)Math.Ceiling(result.TotalCount * 1.0 / request.Pagesize);
            var list=model.OrderByDescending(p=>p.LocationID).Skip((request.Pageindex-1)*request.Pagesize).Take(request.Pagesize).ToList();
            result.Code = ApiENums.成功;
            result.Message = "成功";
            result.list = mapper.Map<List<WHouseLocaDTO>>(list);
            return await Task.FromResult(result);
        }
    }
}
